﻿using SLClient.Extension;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace SLClient.CustomerControls
{
    public partial class NavControl : UserControl
    {
        private NavUserInfo _root;
        private LowerDataEventHandler _LowerClicked;

        public event LowerDataEventHandler LowerClicked
        {
            add
            {
                LowerDataEventHandler handler2;
                LowerDataEventHandler lowerClicked = this._LowerClicked;
                do
                {
                    handler2 = lowerClicked;
                    LowerDataEventHandler handler3 = (LowerDataEventHandler)Delegate.Combine(handler2, value);
                    lowerClicked = Interlocked.CompareExchange<LowerDataEventHandler>(ref this._LowerClicked, handler3, handler2);
                }
                while (lowerClicked != handler2);
            }
            remove
            {
                LowerDataEventHandler handler2;
                LowerDataEventHandler lowerClicked = this._LowerClicked;
                do
                {
                    handler2 = lowerClicked;
                    LowerDataEventHandler handler3 = (LowerDataEventHandler)Delegate.Remove(handler2, value);
                    lowerClicked = Interlocked.CompareExchange<LowerDataEventHandler>(ref this._LowerClicked, handler3, handler2);
                }
                while (lowerClicked != handler2);
            }
        }

        public NavControl()
        {
            this.InitializeComponent();
            NavUserInfo info = new NavUserInfo
            {
                UserId = ViewDispatcher.LoginUser.UserId,
                UserName = "本级"
            };
            this._root = info;
        }

        private void block_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this._LowerClicked != null)
            {
                this._LowerClicked(this, (sender as TextBlock).Tag as NavUserInfo);
            }
        }

        public void SetNavigation(List<NavUserInfo> list)
        {
            this.SetNavigation(list, "");
        }

        public void SetNavigation(List<NavUserInfo> list, string record)
        {
            TextBlock block;
            int num;
            if (string.IsNullOrEmpty(record))
            {
                block = new TextBlock();
                block.Text=this._root.UserName;
                this.LayoutRoot.Children.Clear();
                this.LayoutRoot.Children.Add(block);
                if ((list != null) && (list.Count > 0))
                {
                    block.Tag=this._root;
                    block.Style = Application.Current.Resources["TextBlockStyle0"] as Style;
                    block.MouseLeftButtonUp += new MouseButtonEventHandler(block_MouseLeftButtonUp);
                    for (num = 0; num < list.Count; num++)
                    {
                        block = new TextBlock();
                        this.LayoutRoot.Children.Add(block);
                        block.Style=Application.Current.Resources["TextBlockStyle2"] as Style;
                        block.Text=(">");
                        block = new TextBlock();
                        this.LayoutRoot.Children.Add(block);
                        block.Text=list[num].UserName;
                        if (num == (list.Count - 1))
                        {
                            block.Style=Application.Current.Resources["TextBlockStyle1"] as Style;
                        }
                        else
                        {
                            block.Style=Application.Current.Resources["TextBlockStyle0"] as Style;
                            block.Tag=list[num];
                            block.MouseLeftButtonUp += new MouseButtonEventHandler(block_MouseLeftButtonUp);
                        }
                    }
                }
                else
                {
                    block.Style = Application.Current.Resources["TextBlockStyle1"] as Style;
                }
            }
            else
            {
                block = new TextBlock();
                block.Tag=this._root;
                block.Style=Application.Current.Resources["TextBlockStyle0"] as Style;
                block.MouseLeftButtonUp += new MouseButtonEventHandler(block_MouseLeftButtonUp);
                block.Text=this._root.UserName;
                this.LayoutRoot.Children.Clear();
                this.LayoutRoot.Children.Add(block);
                if ((list != null) && (list.Count > 0))
                {
                    for (num = 0; num < Enumerable.Count<NavUserInfo>(list); num++)
                    {
                        block = new TextBlock();
                        this.LayoutRoot.Children.Add(block);
                        block.Style=Application.Current.Resources["TextBlockStyle2"] as Style;
                        block.Text=">";
                        block = new TextBlock();
                        this.LayoutRoot.Children.Add(block);
                        block.Text=list[num].UserName;
                        block.Style=Application.Current.Resources["TextBlockStyle0"] as Style;
                        block.Tag=list[num];
                        block.MouseLeftButtonUp += new MouseButtonEventHandler(block_MouseLeftButtonUp);
                    }
                }
                block = new TextBlock();
                this.LayoutRoot.Children.Add(block);
                block.Style=Application.Current.Resources["TextBlockStyle2"] as Style;
                block.Text=">";
                block = new TextBlock();
                block.Style=Application.Current.Resources["TextBlockStyle1"] as Style;
                this.LayoutRoot.Children.Add(block);
                block.Text=record;
            }
        }

        public NavUserInfo Root
        {
            get
            {
                if (this._root == null)
                {
                    this._root = new NavUserInfo();
                    this._root.UserId = ViewDispatcher.LoginUser.UserId;
                    this._root.UserName = "本级";
                }
                return this._root;
            }
        }

        public delegate void LowerDataEventHandler(object sender, NavUserInfo nui);
    }
}
